<?php

require_once(APP_URL.'protected/lib/qiniu/autoload.php');
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use Qiniu\Storage\BucketManager;

class QManager {

    public $accessKey = '-FcUKCRcXDp7WrCq72wIPXNc0H7dSHTY65ew35GD';
    public $secretKey = 'RkqHeDf14I5FUf2AJ72x-_XzLYrUjoEceC5JHmlW';
    public $bucket = null;
    public $uploadKey = null;
    public $uploadToken = null;
    public $auth = null;
    public $policy = null;

    public function __construct($bucket, $policy, $key) {

        $this->bucket = $bucket;
        $this->policy = $policy;
        $this->uploadKey = $key;
        $this->auth = new Auth($this->accessKey, $this->secretKey);

        $this->setToken();
    }

    /**
     *
     */
    public function setToken() {

        if($this->policy) {
            $this->uploadToken = $this->auth->uploadToken($this->bucket, $this->uploadKey, TOKEN_VALID_TIME, $this->policy);
        } else {
            $this->uploadToken = $this->auth->uploadToken($this->bucket);
        }
    }

    /**
     * @return null
     */
    public function getToken() {

        return $this->uploadToken;
    }

    public function createDir($serverPath, $localPath) {

        $uploadMgr = new UploadManager();
        list($ret, $err) = $uploadMgr->putFile($this->uploadToken, $serverPath, $localPath);
        echo "\n====> putFile result: \n";
        if ($err !== null) {
            var_dump($err);
        } else {
            var_dump($ret);
        }
    }

    public function downloadFile($serverPath, $localPath) {

        $uploadMgr = new UploadManager();
        list($ret, $err) = $uploadMgr->putFile($this->uploadToken, $serverPath, $localPath);
        echo "\n====> putFile result: \n";
        if ($err !== null) {
            var_dump($err);
        } else {
            var_dump($ret);
        }
    }

    /**
     * @param $serverPath
     * @param $localPath
     */
    public function uploadFile($serverPath, $localPath) {

        $uploadMgr = new UploadManager();
        list($ret, $err) = $uploadMgr->putFile($this->uploadToken, $serverPath, $localPath);
        if ($err !== null) {
            var_dump($err);
        } else {
            var_dump($ret);
        }
    }

    /**
     * @param $serverFile
     * @param $stream
     * @return bool
     */
    public function uploadStream($serverFile, $stream) {

        $uploadManager = new UploadManager();
        list($ret, $err) = $uploadManager->put($this->uploadToken, $serverFile, $stream);
        if ($err !== null) {
            return false;
        }
        return $ret;
    }

    /**
     * @param $fileName
     * @return bool
     */
    public function deleteFile($fileName) {

        $bucketManager = new BucketManager($this->auth);
        $err = $bucketManager->delete($this->bucket, $fileName);
        if($err !== null) {
            return false;
        }
        return true;
    }

}